local skel = fk.CreateSkill {
  name = "efengqi__jueyun",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["efengqi__jueyun"] = "谲云",
  [":efengqi__jueyun"] = "锁定技，你的回合有第七个阶段，视为任一本回合你手牌数变化过的阶段；此阶段结束时，你令手牌数最少的一名角色摸至体力上限。",
  ["#efengqi__jueyun-choice"] = "谲云：请选择你将进行的第七个阶段",
  ["#efengqi__jueyun-choose"] = "谲云：你令手牌数最少的一名角色摸至体力上限",
  ["#FengqiJueyunPhase"] = "%from 获得一个 %arg",

  ["$efengqi__jueyun1"] = "建上昶水城，以逼夏口！",
  ["$efengqi__jueyun2"] = "通川聚粮，伐吴之业，当步步为营。",
}

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local current_event = player.room.logic:getCurrentEvent() -- 当前的这个阶段
      local turn_event = current_event:findParent(GameEvent.Turn, true)
      if turn_event == nil then return false end
      local turn = turn_event.data
      if turn.phase_index ~= 6 then return false end -- 在第六个阶段结束时检测
      local phases = {}
      local all_phases = player.room.logic:getEventsOfScope(GameEvent.Phase, 99, function (e)
        return e.data.who == player
      end, Player.HistoryTurn)
      if #all_phases == 0 then return true end
      player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        -- 获取移动事件所在阶段
        local phase ---@type GameEvent.Phase
        for _, phase_event in ipairs(all_phases) do
          -- 注意，若此阶段为当前阶段，因为当前阶段流程未结束，无法获取正确end_id
          if e.id > phase_event.id and (e.id < phase_event.end_id or phase_event == current_event) then
            phase = phase_event
            break
          end
        end
        if not phase then return false end
        -- 是否失去牌
        if table.find(e.data, function(move)
          return ((move.from == player and table.find(move.moveInfo, function(info)
            return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
          end)) or (move.to == player and move.toArea == Card.PlayerHand)) ~= nil
        end) then
          table.removeOne(all_phases, phase)
          table.insertIfNeed(phases, Util.PhaseStrMapper(phase.data.phase))
        end
        return #all_phases == 0
      end, Player.HistoryTurn)
      event:setCostData(self, phases)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local phases = event:getCostData(self)
    if #phases == 0 then return false end -- 空发
    local room = player.room
    local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
    if turn_event == nil then return false end
    local turn = turn_event.data
    local choice = room:askToChoice(player, {skill_name = skel.name, choices = phases, prompt = "#efengqi__jueyun-choice" })
    room:sendLog{ type = "#FengqiJueyunPhase", from = player.id, arg = choice, toast = true }
    local phase = Util.PhaseStrMapper(choice)
    local phase_data = turn.phase_table[7]
    if phase_data == nil then
      turn.phase_table[7] = PhaseData:new{
        who = player,
        reason = skel.name,
        phase = phase,
      }
    else -- 如果原本就有第七阶段，那就修改数据
      phase_data.phase = phase
    end
  end,
})

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if turn_event == nil then return false end
      local turn = turn_event.data
      return turn.phase_index == 7
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local min = player:getHandcardNum()
    for _, p in ipairs(room.alive_players) do
      min = math.min(min, p:getHandcardNum())
    end
    local targets = table.filter(room.alive_players, function (p) return p:getHandcardNum() == min end)
    if #targets == 0 then return false end
    local tos = room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = targets, skill_name = skel.name,
      prompt = "#efengqi__jueyun-choose",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local to = event:getCostData(self).tos[1]---@type ServerPlayer
    local x = to.maxHp - to:getHandcardNum()
    if x > 0 then
      to:drawCards(x, skel.name)
    end
  end,
})

return skel
